package com.analysis.controller;

import com.analysis.dto.ApiResponse;
import com.analysis.entity.ProductDomain;
import com.analysis.enums.RequireLogin;
import com.analysis.service.ProductDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/product")
@CrossOrigin(origins = "*")
public class ProductDomainController {

    @Autowired
    private ProductDomainService productDomainService;

    @PostMapping("/add")
    @RequireLogin
    public ApiResponse<ProductDomain> createProductDomain(@RequestBody ProductDomain productDomain) {
        try {
            int result = productDomainService.createProductDomain(productDomain);
            if (result > 0) {
                return ApiResponse.success(productDomain);
            } else {
                return ApiResponse.error("品牌域名创建失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("创建品牌域名时发生错误: " + e.getMessage());
        }
    }

    @PostMapping("/delete")
    @RequireLogin
    public ApiResponse<String> deleteProductDomain(Long id) {
        try {
            int result = productDomainService.deleteProductDomain(id);
            if (result > 0) {
                return ApiResponse.success("品牌域名删除成功");
            } else {
                return ApiResponse.error("品牌域名不存在或删除失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("删除品牌域名时发生错误: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    @RequireLogin
    public ApiResponse<ProductDomain> updateProductDomain(@RequestBody ProductDomain productDomain) {
        try {
            productDomain.setId(productDomain.getId()); // 确保ID设置正确
            int result = productDomainService.updateProductDomain(productDomain);
            if (result > 0) {
                ProductDomain updatedProductDomain = productDomainService.getProductDomainById(productDomain.getId());
                return ApiResponse.success(updatedProductDomain);
            } else {
                return ApiResponse.error("品牌域名不存在或更新失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("更新品牌域名时发生错误: " + e.getMessage());
        }
    }

    @GetMapping("/get")
    @RequireLogin
    public ApiResponse<ProductDomain> getProductDomainById(Long id) {
        try {
            ProductDomain productDomain = productDomainService.getProductDomainById(id);
            if (productDomain != null) {
                return ApiResponse.success(productDomain);
            } else {
                return ApiResponse.error("品牌域名不存在");
            }
        } catch (Exception e) {
            return ApiResponse.error("查询品牌域名时发生错误: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    @RequireLogin
    public ApiResponse<List<ProductDomain>> getAllProductDomains() {
        try {
            List<ProductDomain> productDomains = productDomainService.getAllProductDomains();
            return ApiResponse.success(productDomains);
        } catch (Exception e) {
            return ApiResponse.error("查询品牌域名列表时发生错误: " + e.getMessage());
        }
    }

    @GetMapping("/page")
    @RequireLogin
    public ApiResponse<Map<String, Object>> getProductDomainsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        try {
            List<ProductDomain> productDomains = productDomainService.getProductDomainsByPage(pageNum, pageSize);
            int total = productDomainService.countAllProductDomains();

            Map<String, Object> data = new HashMap<>();
            data.put("list", productDomains);
            data.put("total", total);
            data.put("pageNum", pageNum);
            data.put("pageSize", pageSize);

            return ApiResponse.success(data);
        } catch (Exception e) {
            return ApiResponse.error("分页查询品牌域名时发生错误: " + e.getMessage());
        }
    }
}
